Feature: Handle symbol-to-proc wrappers (&:) that refer to a method created using delegation or method missing#406
Conversation
2b8851e to
be995c1
Compare
Danger ReportNo issues found. |
numbata
left a comment
There was a problem hiding this comment.
@marcrohloff Thanks for the contribution! The motivation is solid. 👍
Two things to address before merging: the arity <= 0 guard is too wide (see inline comment), and the new method_missing in the shared fixture risks making the "undefined method" test fragile. Left suggestions on both.
… created using delegation or method missing These methods have an arity of -1
Add argumenterror specs
… specs Fixes arity math for variadic methods, skips methods with required keyword arguments, extracts positional_arity_for for clarity, and adds specs for optional kwargs, splats, private methods, delegation, and a canary for the Proc#to_s regex. Also pins the ActiveSupport delegation require in spec_helper and moves the CHANGELOG entry under Fixes.
d8f4809 to
a4d280f
Compare
|
Thanks for taking my comments into account, @marcrohloff! I added a few improvements on top of your commits before merging:
Great find, and thanks again for the PR! 👍 |
There was a problem hiding this comment.
Pull request overview
This PR improves Grape::Entity’s handling of symbol-to-proc exposure blocks (&:method_name) when the underlying method is wrapped (e.g., ActiveSupport delegate) or dynamically handled (e.g., method_missing), where arity/introspection can be non-standard (including negative arity).
Changes:
- Tighten detection/parsing of symbol-to-proc wrapper procs and adjust arity enforcement logic.
- Add spec coverage for optional args, splats, private methods,
method_missing, anddelegate-backed methods. - Update test setup to load ActiveSupport delegation helpers and document the behavior in the changelog.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
spec/spec_helper.rb |
Requires ActiveSupport delegation extension to support delegate usage in specs. |
spec/grape_entity/entity_spec.rb |
Adds/updates specs for &:method_name across optional args, private methods, method_missing, and delegate. |
lib/grape_entity/entity.rb |
Refines symbol-to-proc wrapper parsing and arity enforcement, with a new helper for arity inspection. |
CHANGELOG.md |
Adds an entry describing the feature. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Switches from Method#arity math to Method#parameters inspection for more precise required-arg detection. Required keyword arguments now produce a curated ArgumentError instead of falling through to Ruby's native error. Adds a spec for the required-kwargs case and sharpens the delegation and method_missing NOTE comments.
|
@numbata Thanks for cleaning that up |
Summary
Improve handling of symbol-to-proc exposure blocks (
&:method_name) for delegated and dynamically dispatched methods.Previously grape-entity eagerly validated method arity for
&:method_nameblocks. This worked for ordinary methods, but produced incorrect behavior for methods implemented viadelegateormethod_missing, where Ruby may expose ambiguous or negative arity values.This PR makes arity validation more conservative and Ruby-compatible:
method_missing-backed methodsChanges
positional_arity_forhelper for normalized arity handlingmethod_missing, splats, kwargs, and edge casesBackward Compatibility
Public API is unchanged.
The only observable behavior change is that delegated or dynamic-dispatch methods may now raise Ruby-native errors instead of grape-entity-generated arity errors when the callable contract cannot be reliably introspected.